package com.kiku.common;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kiku.domain.po.User;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @ClassName PageQuery
 * @Description TODO
 * @Author kiku
 * @Date 2024/6/12 12:58
 * @Version 1.0
 */
@Data
@ApiModel(description = "分页查询实体")
public class PageQuery  {
    @ApiModelProperty("页码")
    private Long pageNo;
    @ApiModelProperty("页码")
    private Long pageSize;
    @ApiModelProperty("排序字段")
    private String sortBy;
    @ApiModelProperty("是否升序")
    private Boolean isAsc;

    public <T> Page<T> toMpPage(OrderItem...items){
        //构建分页条件
        Page<T> userPage = new Page<>(pageNo, pageSize);
        //构建排序条件
        if (sortBy!=null){
            userPage.addOrder(new OrderItem(sortBy,isAsc));

        }
        if (items!=null){
            userPage.addOrder(items);
        }
        return userPage;
    }
    public <T> Page<T> toMpPage(String defaultSortBy, boolean isAsc){
        return this.toMpPage(new OrderItem(defaultSortBy, isAsc));
    }

    public <T> Page<T> toMpPageDefaultSortByCreateTimeDesc() {
        return toMpPage("create_time", false);
    }

    public <T> Page<T> toMpPageDefaultSortByUpdateTimeDesc() {
        return toMpPage("update_time", false);
    }
}